<?php
/**
 * Object Relational Persistence Mapping Class for table: rad_service
 *
 * @package	com.athena
 * @author	LHV
 */
class RadService extends ORDataObject {

	/**#@+
	 * Fields of table: rad_service mapped to class members
	 */
	var $rad_service_id		= '';
	var $rad_group_id		= '';
	var $service_id    		= '';
	var $room_id       		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'rad_service';

	/**
	 * Primary Key
	 */
	var $_key = 'rad_service_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'RadService';

	/**
	 * Handle instantiation
	 */
	function RadService() {
		parent::ORDataObject();
	}

    /**
     * Get radiology services by given group
     *
     * @param int $groupId Group Id
     * @return array List of services
     */
	public function getServicesByGroup($groupId) {
        $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');        
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "t.service_id id, s.$field as name",
            'from'  => "$this->_table t INNER JOIN service s ON (t.service_id=s.service_id AND s.disabled='0')",                            
            'where' => " t.rad_group_id=$groupId",
            'orderby' => "l.$field"
            );
        $ds->setup($this->_db, $sql);

        return $ds->toArray('id', 'name');
    }

    public function getUnMappedServices() {
    	$field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');        
        $type       =   Service::RADIOLOGY_TYPE;
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "s.service_id id, s.$field as name",
            'from'  => "service s
                        LEFT JOIN $this->_table t ON (s.service_id=t.service_id)",                        
            'where' =>  "s.item_type='$type' AND (t.rad_group_id IS NULL OR t.rad_group_id=0)",
            'orderby' => "l.$field"
        );
        $ds->setup($this->_db, $sql);

        return $ds->toArray('id', 'name');
    }
    
    public function getObject($radGroupId, $serviceId){
    	$sql	=	"SELECT $this->_key FROM $this->_table WHERE rad_group_id='$radGroupId' AND service_id='$serviceId'";
    	$res	=	$this->_execute($sql);
    	$obj	=	Celini::newORDO('RadService', $res->fields['$this->_key']);
    	
    	return $obj;

    }
    
	public function deleteServices($radGroupId, $serviceIdList){
		$sql	=	"DELETE FROM $this->_table WHERE rad_group_id='$radGroupId' AND service_id IN ($serviceIdList)";
		$this->_execute($sql);
	}
}
?>
